package com.hxkcloud.patient.aerotel.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.aerotel.util.StringUtil;


/**
 * 确定、取消弹窗
 *
 * @author Alex Huang
 * @email huangyu407@qq.com
 * @date 2014-11-17
 */
public class ConfirmDialog {

    public interface OnConfirmListener {
        void onOk(ConfirmDialog dialog);
    }

    private Context context;
    private AlertDialog mDialog;
    private TextView mTitle;
    private TextView mContent;
    private TextView mConfirm;
    private TextView mCancel;
    private String title;
    private String content;
    private OnConfirmListener listener;
    private boolean canceledOnTouchOutside = true;

    public ConfirmDialog(Context context, String content,
                         final OnConfirmListener listener) {
        this(context, "请确认", content, listener, false);
    }

    public ConfirmDialog(Context context, String title, String content,
                         final OnConfirmListener listener) {
        this(context, title, content, listener, false);
    }

    public ConfirmDialog(Context context, String title, String content,
                         final OnConfirmListener listener, boolean canceledOnTouchOutside) {
        this.context = context;
        this.title = title;
        this.content = content;
        this.listener = listener;
        this.canceledOnTouchOutside = canceledOnTouchOutside;
        mDialog = new AlertDialog.Builder(context).create();
    }

    public void show() {
        mDialog.show();
        mDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        mDialog.setContentView(R.layout.widgets_confirm_dialog);
        mTitle = (TextView) mDialog.findViewById(R.id.title);

        if (StringUtil.isEmpty(title)) {
            mTitle.setVisibility(View.GONE);
        } else {
            mTitle.setText(title);
        }

        mContent = (TextView) mDialog.findViewById(R.id.content);

        if (StringUtil.isEmpty(content)) {
            mContent.setVisibility(View.GONE);
        } else {
            mContent.setText(content);
        }

        mConfirm = (TextView) mDialog.findViewById(R.id.confirm);
        mCancel = (TextView) mDialog.findViewById(R.id.cancel);

        mConfirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onOk(ConfirmDialog.this);
            }
        });

        mCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setConfirmText(String confirmText){
        mConfirm.setText(confirmText);
    }

    public void setCancelText(String cancelText){
        mCancel.setText(cancelText);
    }

    public void setContentSize(float size){
        mContent.setTextSize(size);
    }
    public void dismiss() {
        mDialog.dismiss();
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }
}
